<?php
//module/Shop/src/Shop/Controller/AuthController.php
namespace Shop\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\Session\Container;
use Zend\Form\Annotation\AnnotationBuilder;
use Zend\View\Model\ViewModel;
use Zend\Debug\Debug;
use Zend\Db\Adapter\Adapter;

use Shop\Model\User;

class AuthController extends AbstractActionController
{
	protected $authadapter;
	protected $authservice;
	protected $storage;
	
	public function getAuthAdapter()
	{
		if (! $this->authadapter) {
			$this->authadapter = $this->getServiceLocator()
				->get('DbTableAuthAdapter');
		}
		return $this->authadapter;
	}
	public function getAuthService()
	{
		if (! $this->authservice) {
			$this->authservice = $this->getServiceLocator()
				->get('AuthService');
		}
		return $this->authservice;
	}
	
	public function getSessionStorage()
	{
		if (! $this->storage) {
			$this->storage = $this->getServiceLocator()
			->get('Shop\Model\MyAuthStorage');
		}
		 
		return $this->storage;
	}
	  
	public function loginAction()
	{
		//if already login, redirect to success page 
        if ($this->getAuthService()->hasIdentity()){
            return $this->redirect()->toRoute('shop');
        }
        return new ViewModel(array(
        	'messages'  => $this->flashmessenger()->getMessages(),
        ));
	}
	 
	public function authenticateAction()
	{         
        $request = $this->getRequest();
        if ($request->isPost()){
        	//check authentication...
            $this->getAuthService()->getAdapter()
                                   ->setIdentity($request->getPost('username'))
                                   ->setCredential($request->getPost('password'));
                                        
            $result = $this->getAuthService()->authenticate();
                 
            if ($result->isValid()) {
            	if ($request->getPost('rememberme') == 1 ) {
            		$this->getSessionStorage()->setRememberMe(1);
            		//set storage again
            		$this->getAuthService()->setStorage($this->getSessionStorage());
            	}
            	$this->getSessionStorage()->write($this->getAuthAdapter()->getResultRowObject(
            			null,
            			'password'
            	));
            	$_SESSION['user']['realName'] = $this->getSessionStorage()->read()->real_name;          	
           	}
     	}
     	$this->flashMessenger()->clearMessages();
     	$this->flashMessenger()->addMessage('Tên đăng nhập hoặc mật khẩu không đúng');
        return $this->redirect()->toRoute('auth',array('action' => 'login'));
	}
	 
	public function logoutAction()
	{		
		if(isset($_SESSION['user']['realName']))
			unset($_SESSION['user']['realName']);
		$this->getSessionStorage()->forgetMe();
		$this->getAuthService()->clearIdentity();
		$this->flashMessenger()->clearMessages();
		return $this->redirect()->toRoute('shop');
	}
	
	public function registerAction()
	{
		//if already login, redirect to home page
		if ($this->getAuthService()->hasIdentity()){
			return $this->redirect()->toRoute('shop');
		}
		return new ViewModel();
	}
}